import { option_datetimePicker } from './../../components/def-option.js'
import { CommonPicturePackage } from '../../assets/CommonPicturePackage.js';
import mixinsFun from './mixinsFun.vue';
export default {
  name: 'edit',
  mixins: [mixinsFun],
  components: {
    CusForm: () => { return import('./cusForm.vue'); },
    WxMsg: () => { return import('./../../components/wx_msgArea.vue'); },
  },
  computed: {
    // 一些变量在data里用缩写，computed用后端提交值
    eSCompanyId: function () { return this.esId; },//企业微信ID
    qYWXUsersId: function () { return this.cusId; },//企业微信客户ID
    Lo_MsgList: function () {
      let ls = this.L_MsgList;
      let rs = this.R_MsgList.map((v) => {
        return v.chatMsgId
      })
      let arr = ls.map((v) => {
        return Object.assign(v, {
          isInRight: rs.includes(v.chatMsgId)
        })
      })
      return arr
    },
    Ro_MsgList: function () { return [] },
    chatMsgIdList: function () {
      if (this.R_MsgList.length == 0) {
        return '';
      } else {
        return this.R_MsgList.map((v) => {
          return v.chatMsgId
        }).join(',')
      }//消息列表（提交前转string类型，用','分隔ID）所以实际是string
    }
  },
  data() {
    return {
      isDev: process.env.NODE_ENV == 'development',
      noAgent: CommonPicturePackage.noAgent,
      noData: CommonPicturePackage.noData,
      step: 0,//0:选人，1:编辑，2:提交
      datepicker: (() => {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
        return [start, end]
      })(),
      pickerOptions: option_datetimePicker,
      ceId: (() => { return this.$store.getters.getCEID(true) })(),//员工ID
      rfrId: '',
      esId: null,
      cusId: null,
      rfTitle: '',//标题
      L_MsgList: [],
      R_MsgList: [],
      L_loading: false,
      R_loading: false,
    }
  },
  methods: {
    initGetRFDetails(args) {
      console.log('%c [ args ]-98', 'font-size:13px; background:pink; color:#bf2c9f;', args)
      this.esId = args.esId;
      this.cusId = args.cusId;
      this.rfrId = args.rfrId;
      this.rfTitle = args.rfTitle;
      let arg1 = {
        companyEmplyoeeId: this.ceId,
        companyEmployeeId: this.ceId,
        rFReportFormsId: args.rfrId,
        type: args.type,
        beginTime: '',
        endTime: '',
      }
      this.R_loading = true;
      this.$api.getRFDetails(arg1).then((res) => {
        this.clearMsgList(this.R_MsgList);
        let arr = [];
        if (res.data.data.length > 0) {
          arr = res.data.data.map((v) => {
            return this.msgFormat(v)
          })
        }
        return Promise.resolve(arr);
      }).then((res) => {
        res.forEach((v) => {
          this.R_MsgList.push(Object.assign(v, {
            esName: !v.isMe ? v.esName : v.appName
          }));
        })
        this.R_loading = false;
      }).catch((err) => {
        console.error(err)
      })
    },
    //根据被选人，加载历史记录，步骤2，隐性切换

    // getHistory(){}, // 调接口数据，清空当前数组，填充新数据，现迁移到mixinsFun中复用
    thisGetHistory() {
      if (this.datepicker === null || this.datepicker[0] === '' || this.datepicker[1] === '') {
        this.$message({
          type: 'warning',
          message: '请选择时间范围',
        })
        return
      }
      this.L_loading = true;
      this.getHistory(
        {
          eSCompanyId: this.esId,
          usersId: this.cusId,
          beginTime: this.$moment(this.datepicker[0]).format("YYYY-M-D H:m:s"),
          endTime: this.$moment(this.datepicker[1]).format("YYYY-M-D H:m:s"),
        },
        this.L_MsgList,
        () => { this.L_loading = false },
      )
    },
    selfChecking(action, msg) {
      let target = this.R_MsgList;
      let a = ((action) => {
        return { 'add': 1, 'remove': 2 }[action]
      })(action)// 1:添加消息 2:删除消息
      let idx = target.findIndex((v) => {
        return v.chatMsgId === msg.chatMsgId
      })//定位 被选消息 是否在左侧中
      if (a === 1 && idx >= 0) {
        this.$message({ type: 'warning', message: '请勿重复添加' })
        return true;
      }
      if (a === 2 && idx < -1) {
        this.$message({ type: 'warning', message: '该记录可能不存在' })
        return true;
      }
      if (a === 2 && target.length < 2) {
        this.$message({ type: 'warning', message: '最后一条记录', })
        return true;
      }//移除前判断
      return false;//false:没问题
    },// 自检左右移位的可行性
    realTimeUpdata($event, action, msg) {
      if (this.selfChecking(action, msg)) { return };
      //提交前自检一下逻辑合理不，不要重复提交已经存在的，也不要移除可能不存在的
      let opt = {
        companyEmployeeId: this.ceId,
        companyEmployeeId: this.ceId,
        rFReportFormsId: this.rfrId,
        type: ((action) => {
          return { 'add': 1, 'remove': 2 }[action]
        })(action),// 1:添加消息 2:删除消息
        chatMsgId: msg.chatMsgId// 消息ID
      }
      msg.isLoading = true;
      this.$api.editReportFormsSubmitAction(opt).then((res) => {
        if (res.data.code == 200) {
          if (opt.type === 1) { this.addMsg_R(msg) }
          if (opt.type === 2) { this.removeMsg_R(msg) }
        }
        this.$message({
          type: 'success',
          message: '修改成功',
        })
        msg.isLoading = false;
      }).catch((err) => {
        console.log(err)
        this.$message({
          type: 'error',
          message: '修改失败',
        })
        msg.isLoading = false;
      })
    },
    realTimeUpdataRfTitle() {
      let arg = {
        rFReportFormsId: this.rfrId,
        title: this.rfTitle,
      }
      if (arg.title === '') {
        this.$message({ type: 'error', message: '标题未填写', })
        return Promise.reject()
      }
      return this.$api.updataRfTitle(arg).then((res) => {
        if (res.data.code == 200) {
          this.$message({ type: 'success', message: '标题修改成功', })
          return Promise.resolve(res)
        } else {
          return Promise.reject()
        }


      }).catch((err) => {

      })
    },
    addMsg_R(msg) {
      let $moment = this.$moment;
      let target = this.R_MsgList;
      target.push(
        Object.assign(msg, {
          esName: !msg.isMe ? msg.esName : msg.appName
        })
      );//先插入再排序
      target.sort((a, b) => {
        return $moment(a.addTime).isBefore(b.addTime) ? -1 : 1
      })
    },//将左侧列表的数据加入右侧列表，需去重及排序
    removeMsg_R(msg) {
      let target = this.R_MsgList;
      let idx = target.findIndex((v) => {
        return v.chatMsgId === msg.chatMsgId
      })
      target.splice(idx, 1)
    },//将右侧列表的数据移除

    /*现在这些没用，但组件接口保留*/
    scroll(v) {
      if (v === "Top") { }
      return;
    },//滚动时处理函数，一般收到的参数是Top或Bottom
    /*现在这些没用，但组件接口保留*/
  },
  mounted() {

  }
}

